<script setup lang="ts">
import { computed } from 'vue';
import { $t } from '@/locales';
import { useAntdForm, useFormRules } from '@/hooks/common/form';
import { lockEnum, roleEnum, sexEnum } from '@/enum';

defineOptions({
  name: 'UserSearch'
});
interface Props {
  unitList?: any;
}

defineProps<Props>();
interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const { formRef, validate, resetFields } = useAntdForm();

const model = defineModel<any>('model', {
  required: true
});

type RuleKey = Extract<keyof Api.SystemManage.UserSearchParams, 'userEmail' | 'userPhone'>;

const rules = computed<Record<RuleKey, App.Global.FormRule>>(() => {
  const { patternRules } = useFormRules(); // inside computed to make locale reactive

  return {
    userEmail: patternRules.email,
    userPhone: patternRules.phone
  };
});

async function reset() {
  await resetFields();
  emit('reset');
  emit('search');
}

async function search() {
  await validate();
  emit('search');
}
</script>

<template>
  <ACard :bordered="false" class="card-wrapper">
    <AForm
      ref="formRef"
      :model="model"
      :rules="rules"
      :label-col="{
        span: 5,
        md: 7
      }"
    >
      <ARow :gutter="[16, 16]" wrap>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="账号" name="loginCode" class="m-0">
            <AInput v-model:value="model.loginCode" placeholder="请输入账号" />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="昵称" name="userName" class="m-0">
            <AInput v-model:value="model.userName" placeholder="请输入昵称" />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="手机号" name="telephone" class="m-0">
            <AInput v-model:value="model.telephone" placeholder="请输入手机号" />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="所属单位" name="orgId" class="m-0">
            <ASelect
              v-model:value="model.orgId"
              placeholder="请选择所属单位"
              :options="unitList"
              :field-names="{ label: 'name', value: 'id', options: 'options' }"
              clearable
            />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="用户角色" name="userType" class="m-0">
            <ASelect v-model:value="model.userType" placeholder="请选择用户角色" :options="roleEnum" clearable />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="性别" name="sex" class="m-0">
            <ASelect v-model:value="model.sex" placeholder="请选择性别" :options="sexEnum" clearable />
          </AFormItem>
        </ACol>
        <ACol :span="24" :md="12" :lg="6">
          <AFormItem label="是否锁定" name="locked" class="m-0">
            <ASelect v-model:value="model.locked" placeholder="请选择是否锁定" :options="lockEnum" clearable />
          </AFormItem>
        </ACol>
        <div class="flex-1">
          <AFormItem class="m-0">
            <div class="w-full flex-y-center justify-end gap-12px">
              <AButton @click="reset">
                <template #icon>
                  <icon-ic-round-refresh class="align-sub text-icon" />
                </template>
                <span class="ml-8px">重置</span>
              </AButton>
              <AButton type="primary" ghost @click="search">
                <template #icon>
                  <icon-ic-round-search class="align-sub text-icon" />
                </template>
                <span class="ml-8px">搜索</span>
              </AButton>
            </div>
          </AFormItem>
        </div>
      </ARow>
    </AForm>
  </ACard>
</template>

<style scoped></style>
